<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Document</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/news.css">
    <style>
        html,
        body {
            background: #000;
        }
    </style>
</head>

<body>
    <div id="view" v-cloak></div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/jquery.js"></script>
<script src="../../script/ffkj.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/audio.js"></script>

<script type="text/javascript" src="../../script/lib/lib-generate-test-usersig.min.js"></script>
<script type="text/javascript" src="../../script/lib/md5.js"></script>
<script type="text/javascript" src="../../script/lib/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../script/mhsdk.js"></script>
<script>
    var view = new Vue({
        el: '#view',
        data: {
            type: 0, // 连接情况 -1发起 0 未连接 1已连接
            status: 0, // 后端是否有推送状态过来
            imgurl: imgurl,
            ffList: [],
            ffInfo: {},
            time: '0',
            types: 2, // 1语音|2视频
            myuserid: 0,
            touserid: 0,
            order: '',
        },
        methods: {
            _url(param, url) {
                _url(param, url);
            },
            // 返回图片路径
            returnImg(url) {
                return returnImg(url);
            },
        }
    })
    var rtc;
    var audio;
    apiready = function() {
        api.setKeepScreenOn({
            keepOn: true
        });
        _listener('keyback', function() {})
        if (!$api.getStorage('rtcid')) {
            _alert('缺少视频通话配置，无法进行视频通话', function() {
                api.closeWin();
            });
            return;
        }
        //  需将参数传给videochat_menu
        view.myuserid = $api.getStorage('userid');
        view.touserid = api.pageParam['id'];
        view.type = api.pageParam['type'];
        if (api.pageParam['orderid']) {
            // 发起方推过来的订单id
            view.order = api.pageParam['orderid'];
        }
        showDetail('frame0/videochat_menu', {
            winName: api.pageParam['winName'],
            type: view.type,
            id: view.touserid,
            orderid: view.order,
            intimate: api.pageParam['intimate'] || 0, // 是否需要限制亲密度 1需要 0不需要
        });
        audio = new Audio();
        audio.play({
            url: 'widget://res/tip.mp3',
            loop: 1,
        })
        audio.normal();
        _listener('returnRequest', function() {
            if (audio) {
                audio.stop();
            }
        })


        mhsdkApi.mhInit();
        //设置用户远端视频可见回调
        mhsdkApi.setOnUserVideoAvaialeListener(onUserVideoAvailable);
        var cameraPermission = 'camera'
        var microphonePermission = 'microphone'
        var permissions = [cameraPermission,microphonePermission]
        var resultList = api.hasPermission({
            list: permissions
        });
         if(resultList[0].granted && resultList[1].granted){
            mhsdkApi.startPreview()
        } else{
            api.requestPermission({
                list: permissions,
            }, function(res) {
                if (res.list[0].granted && res.list[1].granted) {
                    mhsdkApi.startPreview()
                }
            });
        }

        enterRoom()



    }





//腾讯TRTC APPId
let sdkAppId =  1400800360
    //腾讯TRTC密码
    let appSecretKey =  'b834144f6a6240cf561b1509c2828587d40e070de1cd6976aca0876b57fd27c6'
    let EXPIRETIME = 604800
    let linkText = '接受连麦'
    let linkFlag = false
    let localVideoText = '本地视频小屏'
    let linkUsers = []
    let linkUserId = ''
    let linkStreamId = ''

    function enterRoom(){
        getPermission(['microphone', 'camera', 'photos'], function(code) {
            if (code != 0) {
                api.execScript({
                    frameName: 'frame0/videochat_menu',
                    script: 'refuse()'
                })
                _msg('请先开启相关权限');
            } else {
                let userId = view.myuserid
                let generator = new LibGenerateTestUserSig(sdkAppId, appSecretKey, EXPIRETIME);
                let userSig = generator.genTestUserSig(userId);

                let roomId = view.myuserid
                let streamId = view.myuserid

                mhsdkApi.enterRoom({
                        roomId:roomId,
                        userId:userId,
                        streamId:streamId,
                        userSig:userSig,
                        localViewBig:0
                    })
            //     setTimeout(function () {
            //         mhsdkApi.startLink({
            //         linkUserId:userId,
            //         linkStreamId:userId,
            //         playUrl:'',
            //         localVideoBig:0
            //     })
            // }, 5000)
            }
        })
    }

    function openMeiYan() {
        mhsdkApi.openMeiYan();
    }

    function onUserVideoAvailable(ret){
        console.log('--onvideo->',ret);
        let data  = ret.detail
        if (data.code == '1'){ //连麦
            if(linkUsers.length == 0){
                linkUserId = data.msg
                linkStreamId = data.msg
            }else{
                linkUserId2 = data.msg
                linkStreamId2 = data.msg
            }
            linkUsers.push(data.msg)
        }else{
            linkUsers.splice(linkUsers.indexOf(data.msg), 1)
        }

    }

    // 加入
    function _join(uid) {

        let generator = new LibGenerateTestUserSig(sdkAppId, appSecretKey, EXPIRETIME);
        let userSig = generator.genTestUserSig(view.myuserid);

        if(uid==view.myuserid){
            mhsdkApi.enterRoom({
                roomId:uid,
                userId:uid,
                streamId:uid,
                userSig:userSig,
                localViewBig:0
            })
            mhsdkApi.startLink({
                    linkUserId:view.touserid,
                    linkStreamId:view.touserid,
                    playUrl:'',
                    localVideoBig:0
            })
        }else{
            mhsdkApi.enterRoom({
                roomId:uid,
                userId:view.myuserid,
                streamId:uid,
                userSig:userSig,
                localViewBig:0
            })
            mhsdkApi.startLink({
                    linkUserId:uid,
                    linkStreamId:uid,
                    playUrl:'',
                    localVideoBig:0
            })
        }


    }
    // 离开
    function _leave() {
        $api.setStorage('callStatus', 0); // 不在通话中
        mhsdkApi.exitRoom({})
    }

    // 切换摄像头
    function switchCamera() {
        rtc.switchCamera();
    }

    function stopAudio() {
        if (audio) {
            audio.stop();
        }
        if (rtc) {
            rtc.startLocalAudio();
        }
        api.bringFrameToFront({
            from: 'frame0/videochat_menu',
        });
    }
</script>

</html>